package com.wish.wishhouses_market.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.wishhouses_market.entity.*;
import com.wish.wishhouses_market.mapper.TbPremisesInfoMapper;
import com.wish.wishhouses_market.mapper.TbRelationPremisesPropertyMapper;
import com.wish.wishhouses_market.service.ITbAlbumService;
import com.wish.wishhouses_market.service.ITbPremisesInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wish.wishhouses_market.service.ITbRelationPremisesPropertyService;
import dto.housesMarketDto.PremisesDto;
import dto.housesMarketDto.PremisesFacilityDto;
import org.apache.commons.collections.ArrayStack;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 楼盘信息表 服务实现类
 * </p>
 *
 * @author fanzijun
 * @since 2021-03-31
 */
@Service
public class TbPremisesInfoServiceImpl extends ServiceImpl<TbPremisesInfoMapper, TbPremisesInfo> implements ITbPremisesInfoService {

    @Autowired
    ITbAlbumService iTbAlbumService;
    @Autowired
    ITbRelationPremisesPropertyService iTbRelationPremisesPropertyService;
    /**
     *
     * @param premisesDto
     * 列表
     */
    @Override
    public List getList(PremisesDto premisesDto) {
        TbPremisesInfo premisesInfo = new TbPremisesInfo();
        BeanUtils.copyProperties(premisesDto,premisesInfo);
        return baseMapper.getList(premisesInfo);
    }

    @Override
    public void save(PremisesDto premisesDto) {
        if (premisesDto.getId()==null){
            TbPremisesInfo premisesInfo = new TbPremisesInfo();
            List<PremisesFacilityDto> peripheralFacilities = premisesDto.getPeripheralFacilities();
            List<PremisesFacilityDto> trafficCondition = premisesDto.getTrafficCondition();
            BeanUtils.copyProperties(premisesDto,premisesInfo);
            // 接受的商圈
            List<Integer> selcirlie = premisesDto.getSelcirlie();
            String shangquan = "";
            for(Integer i : selcirlie){
                shangquan=shangquan+","+i;
            }
            premisesInfo.setCommercialArea(shangquan);
            // 接受的地铁
            List<Integer> selectsubway = premisesDto.getSelectsubway();
            String ditie = "";
            for(Integer i : selectsubway){
                ditie=ditie+","+i;
            }
            premisesInfo.setSubway(ditie);


            // 接受的建筑类型
            List<Integer> Buildings = premisesDto.getBuildingTypes();
            String b = "";
            for(Integer i : Buildings){
                b=b+","+i;
            }
            premisesInfo.setBuildingType(b);

            baseMapper.insert(premisesInfo);

//            // 楼盘特色
//            for (Integer relation :premisesDto.getConfigProperty()){
//                TbRelationPremisesProperty tbRelationPremisesProperty = new TbRelationPremisesProperty();
//                tbRelationPremisesProperty.setPremisesId(premisesInfo.getId());
//                tbRelationPremisesProperty.setPropertyId(relation);
//
//            }
            if(CollectionUtils.isNotEmpty(premisesDto.getConfigProperty())){
                List<TbRelationPremisesProperty> premisesInfos = new ArrayList<>();
                for(String s : premisesDto.getConfigProperty()){
                    TbRelationPremisesProperty premisesFacility = new TbRelationPremisesProperty();
                    premisesFacility.setPremisesId(premisesInfo.getId().toString());
                    premisesFacility.setPropertyId(s);
                    premisesInfos.add(premisesFacility);
                }
                baseMapper.saveConfig(premisesInfos);
            }


            // 周边配套
            if (CollectionUtils.isNotEmpty(premisesDto.getPeripheralFacilities())) {
                List<TbPremisesFacility> facilities = new ArrayList<>();

                for (PremisesFacilityDto premisesFacility : premisesDto.getPeripheralFacilities()) {
                    premisesFacility.setPremisesId(premisesInfo.getId());
                    premisesFacility.setType(0);
                    TbPremisesFacility facility = new TbPremisesFacility();
                    BeanUtils.copyProperties(premisesFacility,facility);
                    facilities.add(facility);
                }
                baseMapper.saveFacility(facilities);

            }

            // 交通情况
            if (CollectionUtils.isNotEmpty(premisesDto.getPeripheralFacilities())) {
                List<TbPremisesFacility> facilities = new ArrayList<>();

                for (PremisesFacilityDto premisesFacility : premisesDto.getPeripheralFacilities()) {
                    premisesFacility.setPremisesId(premisesInfo.getId());
                    premisesFacility.setType(1);
                    TbPremisesFacility facility = new TbPremisesFacility();
                    BeanUtils.copyProperties(premisesFacility,facility);
                    facilities.add(facility);
                }
                baseMapper.saveFacility(facilities);
            }

        }else {
            TbPremisesInfo premisesInfo = new TbPremisesInfo();

            BeanUtils.copyProperties(premisesDto,premisesInfo);

            // 接受的商圈
            List<Integer> selcirlie = premisesDto.getSelcirlie();
            String shangquan = "";
            for(Integer i : selcirlie){
                shangquan=shangquan+","+i;
            }
            premisesInfo.setCommercialArea(shangquan);
            // 接受的地铁
            List<Integer> selectsubway = premisesDto.getSelectsubway();
            String ditie = "";
            for(Integer i : selectsubway){
                ditie=ditie+","+i;
            }
            premisesInfo.setSubway(ditie);

            // 接受的建筑类型
            List<Integer> Buildings = premisesDto.getBuildingTypes();
            String b = "";
            for(Integer i : Buildings){
                b=b+","+i;
            }

            baseMapper.updateById(premisesInfo);
            QueryWrapper wrapper= new QueryWrapper();
            wrapper.eq("premises_id",premisesInfo.getId());
            // 根据 楼盘ID去删除 中间表
            iTbRelationPremisesPropertyService.remove(wrapper);
            //楼盘特色
            if(CollectionUtils.isNotEmpty(premisesDto.getConfigProperty())){
                List<TbRelationPremisesProperty> premisesInfos = new ArrayStack();
                for(String s : premisesDto.getConfigProperty()){
                    TbRelationPremisesProperty premisesFacility = new TbRelationPremisesProperty();
                    premisesFacility.setPremisesId(premisesDto.getId().toString());
                    premisesFacility.setPropertyId(s);
                    premisesInfos.add(premisesFacility);
                }
                baseMapper.saveConfig(premisesInfos);
            }
            // 周边配套
            if (CollectionUtils.isNotEmpty(premisesDto.getPeripheralFacilities())) {
                List<TbPremisesFacility> facilities = new ArrayList<>();

                for (PremisesFacilityDto premisesFacility : premisesDto.getPeripheralFacilities()) {
                    premisesFacility.setPremisesId(premisesInfo.getId());
                    premisesFacility.setType(0);
                    TbPremisesFacility facility = new TbPremisesFacility();
                    BeanUtils.copyProperties(premisesFacility,facility);
                    facilities.add(facility);
                }
                baseMapper.saveFacility(facilities);

            }

            // 交通情况
            if (CollectionUtils.isNotEmpty(premisesDto.getPeripheralFacilities())) {
                List<TbPremisesFacility> facilities = new ArrayList<>();

                for (PremisesFacilityDto premisesFacility : premisesDto.getPeripheralFacilities()) {
                    premisesFacility.setPremisesId(premisesInfo.getId());
                    premisesFacility.setType(1);
                    TbPremisesFacility facility = new TbPremisesFacility();
                    BeanUtils.copyProperties(premisesFacility,facility);
                    facilities.add(facility);
                }
                baseMapper.saveFacility(facilities);
            }

        }


    }

    @Override
    public PremisesDto FindByid(Integer id) {
        TbPremisesInfo tbPremisesInfo = baseMapper.selectById(id);
        PremisesDto premiseDto = new PremisesDto();

        BeanUtils.copyProperties(tbPremisesInfo,premiseDto);

        // 商圈
        if(StringUtils.isNotBlank(tbPremisesInfo.getCommercialArea())){
            List<Integer> list =new ArrayList();
            String[] split = tbPremisesInfo.getCommercialArea().split(",");
            for (String s:split){
                if(StringUtils.isNotBlank(s)){
                    list.add(Integer.valueOf(s));
                }
            }
            premiseDto.setSelcirlie(list);
        }
        // 地铁
        if(StringUtils.isNotBlank(tbPremisesInfo.getSubway())){
            List<Integer> list =new ArrayList();
            String[] split1 = tbPremisesInfo.getSubway().split(",");
            for (String s: split1){
                if(StringUtils.isNotBlank(s)){
                    list.add(Integer.valueOf(s));
                }
            }
            premiseDto.setSelectsubway(list);
        }

        // 建筑类型
        if(tbPremisesInfo.getBuildingType()!=null){
            List<Integer> list =new ArrayList();
            String[] split3 = tbPremisesInfo.getBuildingType().split(",");
            for (String s: split3){
                if(StringUtils.isNotBlank(s)){
                    list.add(Integer.valueOf(s));
                }
            }
            premiseDto.setBuildingTypes(list);
        }

        List<String> list = baseMapper.getProList(id);
        premiseDto.setConfigProperty(list);

        return premiseDto;
    }


}
